package liumengbaio.lolzhangyoubao.Activity.View;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import liumengbaio.lolzhangyoubao.R;


public class MsgDialog extends Dialog {

    public MsgDialog(Context context) {
        super(context);
    }

    public MsgDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private boolean toggle_one,toggle_two,toggle_three;
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private View.OnClickListener switch_onclickListenr;
        private boolean flag = true;

        public boolean isToggle_one() {
            return toggle_one;
        }

        public void setToggle_one(boolean toggle_one) {
            this.toggle_one = toggle_one;
        }

        public boolean isToggle_two() {
            return toggle_two;
        }

        public void setToggle_two(boolean toggle_two) {
            this.toggle_two = toggle_two;
        }

        public boolean isToggle_three() {
            return toggle_three;
        }

        public void setToggle_three(boolean toggle_three) {
            this.toggle_three = toggle_three;
        }

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setDialogOnclick_One(View.OnClickListener listener) {
            this.switch_onclickListenr = listener;
            return this;
        }
        public Builder setDialogOnclick_Two(View.OnClickListener listener) {
            this.switch_onclickListenr = listener;
            return this;
        }
        public Builder setDialogOnclick_Three(View.OnClickListener listener) {
            this.switch_onclickListenr = listener;
            return this;
        }

        public MsgDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final MsgDialog dialog = new MsgDialog(context, R.style.Dialog);
            final View layout = inflater.inflate(R.layout.msg_dialog, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            if (true) {
                if(isToggle_one()){
                    layout.findViewById(R.id.toggle_allday).setBackgroundResource(R.mipmap.user_switch_on);
                    toggle_one = false;
                }else {
                    layout.findViewById(R.id.toggle_allday).setBackgroundResource(R.mipmap.user_switch_off);
                    toggle_one = true;
                }
                setToggle_one(toggle_one);
                layout.findViewById(R.id.toggle_allday).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch_onclickListenr.onClick(v);
                    }
                });
                if(isToggle_two()){
                    layout.findViewById(R.id.close_toggle).setBackgroundResource(R.mipmap.user_switch_on);
                    toggle_two = false;
                }else {
                    layout.findViewById(R.id.close_toggle).setBackgroundResource(R.mipmap.user_switch_off);
                    toggle_two = true;
                }
                setToggle_two(toggle_two);
                layout.findViewById(R.id.close_toggle).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch_onclickListenr.onClick(v);
                    }
                });
                if(isToggle_three()){
                    layout.findViewById(R.id.night_toggle).setBackgroundResource(R.mipmap.user_switch_on);
                    toggle_three = false;
                }else {
                    layout.findViewById(R.id.night_toggle).setBackgroundResource(R.mipmap.user_switch_off);
                    toggle_three = true;
                }
                setToggle_three(toggle_three);
                layout.findViewById(R.id.night_toggle).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch_onclickListenr.onClick(v);
                    }
                });
            } else if (contentView != null)

            {
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content)).addView(
                        contentView, new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
            }

            dialog.setContentView(layout);
            return dialog;
        }

    }
}
